<?php
declare(strict_types=1);
 

namespace App\Fastapp\Mapper;

use App\Fastapp\Model\AccountWithdrawal;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;
use Mine\Helper\Str;

/**
 * 提现列表Mapper类
 */
class FastappAccountWithdrawalMapper extends AbstractMapper
{
    /**
     * @var AccountWithdrawal
     */
    public $model;

    public function assignModel()
    {
        $this->model = AccountWithdrawal::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {

        // 用户id
        if (isset($params['user_id']) && filled($params['user_id'])) {
            $query->where('user_id', '=', $params['user_id']);
        }

        // 币种
        if (isset($params['currency_id']) && filled($params['currency_id'])) {
            $query->where('currency_id', '=', $params['currency_id']);
        }

        // 
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 打卡成功0否1是
        if (isset($params['pay_status']) && filled($params['pay_status'])) {
            $query->where('pay_status', '=', $params['pay_status']);
        }

        // 支付平台账户类型
        if (isset($params['pay_type']) && filled($params['pay_type'])) {
            $query->where('pay_type', '=', $params['pay_type']);
        }

        return $query->orderByDesc('id');
    }

    public function pay(int $id): string
    {
        $md = $this->model::query()->findOrFail($id);
        if (!$md->status) {
            if ($md->code) {
                $cs = '\\App\\Fastapp\\Api\\Service\\Pay\\Platform\\' . Str::studly($md->code) . 'Service';
                try {
                    if ($cs::payOut($md)) {
                        return t('admin.payout_success');
                    }
                } catch (\Throwable) {
                }
            }
            return t('admin.payout_fail');
        }
        return t('admin.payout_end');
    }
}